package com.post.remote.server;

import java.util.Vector;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.post.remote.IRemoteMessageHandler;

public class RemoteService extends Service {
    protected static final String TAG = "RemoteService";
    public final static String KEY_DATA_JSON = "json";
    public final static String KEY_DATA_BYTES = "bytes";
    public static Vector<IRemoteMessageHandler> handlers = new Vector<IRemoteMessageHandler>();
    public static void addHandler(IRemoteMessageHandler pHandler){
        handlers.add(pHandler);
    }
    public static void removeHandler(IRemoteMessageHandler pHandler){
        handlers.remove(pHandler);
    }
    private static Messenger currentClientMessenger;
    public RemoteService() {
    }
    @Override
    public IBinder onBind(final Intent intent) {
        Handler mHandler  = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                final Messenger callback = msg.replyTo;
                currentClientMessenger = callback;
                for (int i = 0; i < handlers.size(); i++) {
                    handlers.get(i).handleMessengerConnect(callback);
                }
                Bundle data = msg.getData();
                byte[] bytes = data.getByteArray(KEY_DATA_BYTES);
                if (bytes!=null){
                    for (int i = 0; i < handlers.size(); i++) {
                        handlers.get(i).handleMessage(msg.arg1,bytes);
                    }
                }
                final String string = data.getString(KEY_DATA_JSON);
                if (!TextUtils.isEmpty(string)){
//                    Log.d(TAG, "RemoteService->recv msg:" + string);
                    for (int i = 0; i < handlers.size(); i++) {
                        handlers.get(i).handleMessage(string);
                    }
                }
            }
        };
        final Messenger messenger = new Messenger(mHandler);
        return messenger.getBinder();
    }

    @Override
    public void onDestroy() {
        //被杀死的时候重启
        Intent sevice = new Intent(this, RemoteService.class);
        this.startService(sevice);
        super.onDestroy();
    }
    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
    public static boolean sendMsg(Messenger pMsger,int pAction,byte[] pBytes){
        if (currentClientMessenger==null){
            Log.w(TAG,"RemoteService ->NO client connected ");
            return false;
        }
        if (pBytes==null||pBytes.length<=0) {
            Log.w(TAG,"RemoteService ->bytes is null");
            return false;
        }
        Message msg =  Message.obtain();
        msg.arg1 = pAction;
        msg.getData().putByteArray(KEY_DATA_BYTES, pBytes);
//        msg.getData().putInt("action",action);
        try {
            currentClientMessenger.send(msg);
            return true;
        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        }
    }
    public static boolean sendMsg(Messenger pMsger,String pJsonMsg){
        if (currentClientMessenger==null){
            Log.w(TAG,"RemoteService ->NO client connected ");
            return false;
        }
        if (TextUtils.isEmpty(pJsonMsg)) {
            Log.w(TAG,"RemoteService ->pJsonMsg is null");
            return false;
        }
        Message msg =  Message.obtain();
        msg.getData().putString(KEY_DATA_JSON, pJsonMsg);
        try {
            currentClientMessenger.send(msg);
            return true;
        } catch (RemoteException e) {
            e.printStackTrace();
            return false;
        }
    }
}
